soda()
	._page('iam_role')
	._module(['dtree'])
	._api('/v1/iam/role')
	._query(['name^like^', 'state^eq^-1'])
	._orderBy(['grade^desc'])
	._run((unit) => {
		// api配置
		unit.api['permission'] = '/v1/iam/role/permission';
		
		// 表单配置
		unit.templateForm['verify'] = ["name"];
		unit.templateForm['items'] = [
			{code: 1, name:'name', title:'角色名称', placeholder:'请输入角色名称', attr: 'maxlength=20'},
			{code: 1, name:'grade', title:'等级', type: 'number',attr: 'max=5 ', placeholder:'角色等级 1-5数字越小优先级越大', },
			{code: 2, name:'isDefault', title:'默认角色', options: unit.globalConstant['dataDefault']},
			{code: 2, name:'state', title:'状态', options: unit.globalConstant['enableState']},
			{code: 5, type:'text', name:'remark', title:'备注', placeholder:'备注信息'}
		];
		
		// 表格基础配置
		unit.templateTable['cols'] = [
			{fixed: 'left', field: 'name', title: '角色名称', minWidth: 120},
			{field: 'grade', title: '等级'},
			{field: 'isDefault', title: '默认',templet(obj) {
			    return unit.sdFormat['json']['get'](unit.globalConstant['dataDefault'], obj.isDefault, 'value').title;
			}},
			{field: 'state', title: '状态',templet(obj) {
			    return unit.sdFormat['json']['get'](unit.globalConstant['enableState'], obj.state, 'value').title;
			}},
			{field: 'createTime', title: '创建时间'},
			{field: 'updateTime', title: '上次修改', templet(obj) {
			    let ct = obj.createTime;
			    let ut = obj.updateTime;
			    return ct == ut ? '' : ut;
			}},
			{fixed: 'right', title: '操作', minWidth: 180, toolbar: unit.sysApplication['btns']['right']()},
		];
		unit.templateTable['bar'] = (bar) => {
			unit.templateDialog[bar.event]();
		}
		unit.templateTable['tool'] = (tool) => {
			unit.templateDialog[tool.event](tool.data);
		}
	
		// 弹出层
		unit.templateDialog['form'] = (data) => {
			unit.sdDialog.form({
				title: data ? '修改' : "创建",
				height: '500px',
				init() {
					unit.sdForm['init']['form'](unit.templateForm, data);
				},
				opt(index) {
					// 获取form
					let form = unit.sdForm['get'](unit.el['form'], unit.templateForm['verify']);
					
					// 校验数据
					if (!form.check) {
						unit.sdDialog['tips']['warning'](form.message);
						return false;
					}
					
					// 数据处理
					let val = form.data;
					val['state'] = val['state'] || 1;
					val['grade'] = val['grade'] || 3;
			
					let param = {
						url: unit.api['crud'],
						data: val,
						tips: true,
						callback() {
							unit.sdDialog['close'](index);
							unit.sdTable['reload'](unit.el.table);
						}
					}
					// 操作
					data ? unit.sdHttp['put'](param, data.id) : unit.sdHttp['post'](param);
				}
			})
		}
		unit.templateDialog['create'] = unit.templateDialog['form'];
		unit.templateDialog['update'] = unit.templateDialog['form'];
		unit.templateDialog['delete'] = (data) => {
			unit.sdDialog.confirm({
				title: "是否要删除这条数据?",
				submit() {
					let param = {
						url: unit.api['crud'],
						callback() {
							unit.sdTable['reload'](unit.el.table)
						}
					}
					unit.sdHttp['delete'](param, data.id);
				}
			});
		}
		unit.templateDialog['permission'] = (data) => {
			unit.sdDialog.page({
				title: "权限配置",
				url: '/components/iam/application/permission.html',
				init: function (){
				    let param = {
				        url: unit.api['permission'],
				        callback: function (d) {
				            unit.dtree.render({
				                elem: '#app-auth-tree',
				                data: d,
				                type: "all",
				                checkbar:true,
				                initLevel: 3,
				                checkbarType: "all",
				                checkbarData: "choose",
				                dot: false,
				                skin: "zdy"
				            });
				        }
				    }
				   unit.sdHttp['get'](param, data.id);
				},
				opt: function (index) {
				    let auths = unit.dtree.getCheckbarNodesParam("app-auth-tree");
				    let param = {
				        url: unit.api['permission'],
				        data: auths,
				        tips: true,
				        callback: function (d) {
							unit.sdDialog['close'](index);
				        }
				    }
				    unit.sdHttp['put'](param, data.id);
				}
			});
		}
		unit.sdTable['render'](unit.templateTable);
	})
